package net.caixiaomi.info.ui.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;

import com.baidu.mobstat.StatService;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.CommonFragmentPageAdapter;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.CommonTab;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import icepick.State;

/**
 * 投注记录
 */
public class OrderRecordActivity extends BaseActivity {

    @State
    int mPosition;

    @BindView(R.id.tab)
    TabLayout mTabLayout;
    @BindView(R.id.pager)
    ViewPager mViewPager;
    private CommonFragmentPageAdapter mAdapter;
    private TabLayout.TabLayoutOnPageChangeListener mChangeListener = new TabLayout.TabLayoutOnPageChangeListener(mTabLayout) {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            AppHelper.sendStatistics(position == 0 ? "tzjlquanbu" : position == 1 ? "tzjlzhongjiang" : "tzjldaikaijiang");
            mPosition = position;
        }
    };

    @Override
    protected int layout() {
        return R.layout.act_coupons;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] tabArray = getResources().getStringArray(R.array.TAB_PURCHASE_RECORD_ARRAY);
        List<Fragment> data = new ArrayList<>();
        List<CommonTab> titles = new ArrayList<>();
        for (int index = 0; index < tabArray.length; index++) {
            OrderRecordFragment fragment = new OrderRecordFragment();
            CommonTab tab = new CommonTab(index, tabArray[index]);
            if (index == 0) {
                tab.setType(-1);
            } else if (index == 1) {
                tab.setType(5);
            } else if (index == 2) {
                tab.setType(3);
            }
            titles.add(tab);
            fragment.setType(tab.getType());
            data.add(fragment);
        }

        mAdapter = new CommonFragmentPageAdapter(getSupportFragmentManager(), titles, data);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.layout_divider_vertical));
        mViewPager.addOnPageChangeListener(mChangeListener);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setCurrentItem(mPosition);
//        mTabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                AppHelper.setIndicator(mTabLayout);
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "touzhujilu-view");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        StatService.onPageEnd(this, "touzhujilu-view");
    }

    @Override
    public void onBackPressed() {
        AppHelper.sendStatistics("tzjlfanhui");
        super.onBackPressed();
    }
}
